package com.mingrisoft.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
/**
 * 
* @ClassName: CountfFilter 
* @Description: TODO(这里用一句话描述这个类的作用) 计数器
* @author zhengzengliang 
* @date 2016年12月20日 下午8:13:02 
*
 */
public class CountFilter implements Filter{

	private int count;
	
	public CountFilter() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, 
			FilterChain chain)throws IOException, ServletException {
		// TODO Auto-generated method stub
		count++; //访问量自增
		//将ServletRequest转换成HttpServletRequest   为什么？？？？？？？？？
		HttpServletRequest req = (HttpServletRequest) request;
		//获取ServletContext
		ServletContext context = req.getSession().getServletContext();
		//将来访数量值放入到ServletContext中
		context.setAttribute("count", count);
		//向下传递过滤器
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig fc) throws ServletException {
		// TODO Auto-generated method stub
		String param = fc.getInitParameter("count");
		count = Integer.valueOf(param);
	}

}
